package com.goldcn.sys.controller;

import com.goldcn.common.CommonUtils;
import com.goldcn.common.RSAUtils;
import com.goldcn.sys.dto.LoginDto;
import com.goldcn.sys.model.util.ValidateCode;
import com.goldcn.sys.service.RedisService;
import com.goldcn.sys.service.SysUserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.PrivateKey;

/**
 * Created by Administrator on 2016/9/3.
 */
@RestController
@RequestMapping(value = "sysUser")
@Api(value = "登录", description = "用户登录")
public class UserLoginController {
    @Autowired
    SysUserLoginService loginService;

    @Autowired
    RedisService redisService;

    /**
     * 登录，需要ras加密
     */
    @RequestMapping(value = "/userLogin", method = RequestMethod.POST)
    public Object login(@RequestBody LoginDto loginDto, HttpServletRequest request) throws Exception {
        //从redis获取私钥并解密
        PrivateKey key = (PrivateKey) redisService.loadKey(loginDto.getRasId());
        byte[] en_result = RSAUtils.hexStringToBytes(loginDto.getPassWord());
        byte[] de_result = RSAUtils.decrypt(key, en_result);
        StringBuffer sb = new StringBuffer();
        sb.append(StringUtils.newStringUtf8(de_result));
        loginDto.setPassWord(CommonUtils.MD5(sb.reverse().toString()));
        return loginService.doLogin(loginDto, request);
    }

    @ApiOperation(value = "验证码")
    @RequestMapping(value = "valiCode", method = RequestMethod.GET)
    public void getCode(HttpServletRequest reqeust,
                        HttpServletResponse response) throws ServletException, IOException {
        // 设置响应的类型格式为图片格式
        response.setContentType("image/jpeg");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        HttpSession session = reqeust.getSession();
        System.out.println("getcode="+session.getId());
        ValidateCode vCode = new ValidateCode(120, 40, 4, 100);
        session.setAttribute("code", vCode.getCode());
        vCode.write(response.getOutputStream());
    }


    /**
     * 获取公钥
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/rasKey", method = RequestMethod.GET)
    @ResponseBody
    public Object getRASKey() throws Exception {
        return loginService.getRASKey();
    }

    /**
     * 用户退出
     *
     * @param userID
     * @return
     */
    @RequestMapping(value = "/loginOut", method = RequestMethod.POST)
    public Object loginOut(HttpServletRequest request, String userID, String platform) {
        return loginService.loginOut(request,userID,platform);
    }

    /**
     * 忘记密码，修改密码
     */
    @RequestMapping(value = "/passWord", method = RequestMethod.POST)
    public Object updatePassWord(@RequestBody LoginDto loginDto) throws Exception {
        PrivateKey key = (PrivateKey) redisService.loadKey(loginDto.getRasId());
        byte[] oldDn_result = RSAUtils.hexStringToBytes(loginDto.getOldPassWord());
        byte[] odlDe_result = RSAUtils.decrypt(key, oldDn_result);
        StringBuffer oldSb = new StringBuffer();
        oldSb.append(StringUtils.newStringUtf8(odlDe_result));
        loginDto.setOldPassWord(CommonUtils.MD5(oldSb.reverse().toString()));
        byte[] en_result = RSAUtils.hexStringToBytes(loginDto.getPassWord());
        byte[] de_result = RSAUtils.decrypt(key, en_result);
        StringBuffer sb = new StringBuffer();
        sb.append(StringUtils.newStringUtf8(de_result));
        loginDto.setPassWord(CommonUtils.MD5(sb.reverse().toString()));
        return loginService.updatePassWord(loginDto);
    }

}
